package com.winshare.saleAssistant.service.impl;

import com.winshare.saleAssistant.domain.entity.StaffDO;
import com.winshare.saleAssistant.domain.entity.SysParamDO;
import com.winshare.saleAssistant.service.RedisService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.redis.core.HashOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.util.concurrent.TimeUnit;

@Service
public class RedisServiceImpl implements RedisService {

    private final static Logger logger = LoggerFactory.getLogger(RedisServiceImpl.class);

    private final RedisTemplate redisTemplate;

    private final static String captchaPre = "captchaPre";

    private final static String tokenPre = "tokenPre";

    private final static String sysParamPre = "sysParam";

    public RedisServiceImpl(RedisTemplate redisTemplate) {
        this.redisTemplate = redisTemplate;
    }


    @Override
    public void putCaptchaCode(String phone, String captchaCode) {
        redisTemplate.opsForValue().set(getCaptchaCodeKey(phone), captchaCode);
        logger.info("手机{}存放验证码：{}", phone, captchaCode);
    }

    @Override
    public String getCaptchaCode(String phone) {
        return redisTemplate.opsForValue().get(getCaptchaCodeKey(phone)).toString();
    }

    @Override
    public void expireCaptchaCode(String phone) {
        redisTemplate.delete(getCaptchaCodeKey(phone));
    }

    @Override
    public void setCurrentUserInfo(String token, StaffDO staffDO, Integer timeOutTime) {
        HashOperations<String, String, Object> hashOperations = redisTemplate.opsForHash();
        hashOperations.put(tokenKey(token), "account", staffDO.getAccount());
        hashOperations.put(tokenKey(token), "staffName", staffDO.getStaffName());
        hashOperations.put(tokenKey(token), "orgId", staffDO.getOrgId());
        hashOperations.put(tokenKey(token), "orgName", staffDO.getOrgName());
        hashOperations.put(tokenKey(token), "orgCode", staffDO.getOrgCode());
        hashOperations.put(tokenKey(token), "id", staffDO.getId());
        hashOperations.put(tokenKey(token), "phone", staffDO.getPhone());
        hashOperations.put(tokenKey(token), "openId", staffDO.getOpenId());
        if (logger.isInfoEnabled()) {
            String currentTime = LocalDateTime.now(ZoneId.of("Asia/Shanghai"))
                    .format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
            logger.info("{}重新登录，登录时间为：", staffDO.getStaffName(), currentTime);
        }
        tokenExpireTimeUpdate(token, timeOutTime);
    }

    @Override
    public StaffDO getCurrentUserInfo(String token) {
        if (redisTemplate.hasKey(tokenKey(token))) {
            StaffDO staffDO = new StaffDO();
            staffDO.setAccount(redisTemplate.opsForHash().get(tokenKey(token), "account").toString());
            staffDO.setStaffName(redisTemplate.opsForHash().get(tokenKey(token), "staffName").toString());
            staffDO.setId((Integer) redisTemplate.opsForHash().get(tokenKey(token), "id"));
            staffDO.setPhone(redisTemplate.opsForHash().get(tokenKey(token), "phone").toString());
            staffDO.setOrgCode(redisTemplate.opsForHash().get(tokenKey(token), "orgCode").toString());
            staffDO.setOrgName(redisTemplate.opsForHash().get(tokenKey(token), "orgName").toString());
            staffDO.setOrgId((Integer) redisTemplate.opsForHash().get(tokenKey(token), "orgId"));
            Object openId = redisTemplate.opsForHash().get(tokenKey(token), "openId");
            if (StringUtils.isEmpty(openId)) {
                staffDO.setOpenId(null);
            } else {
                staffDO.setOpenId(redisTemplate.opsForHash().get(tokenKey(token), "openId").toString());
            }
            return staffDO;
        }
        return null;
    }

    @Override
    public void tokenExpireTimeUpdate(String token, Integer timeOut) {
        redisTemplate.expire(tokenKey(token), timeOut, TimeUnit.SECONDS);
    }

    @Override
    public void saveSysParam(SysParamDO sysParamDO) {
        HashOperations<String, String, Object> hashOperations = redisTemplate.opsForHash();
        hashOperations.put(sysParamPre, "isNecessaryPassword", sysParamDO.getIsNecessaryPassword());
        hashOperations.put(sysParamPre, "passwordMinBit", sysParamDO.getPasswordMinBit());
        hashOperations.put(sysParamPre, "onlineTime", sysParamDO.getOnlineTime());
    }

    private String getCaptchaCodeKey(String phone) {
        return captchaPre + ":" + phone;
    }

    private String tokenKey(String token) {
        return tokenPre + ":" + token;
    }

}
